#include "pool_head.h"
#include "command.h"

void *thread_handler(void *args){
    //强转类型
    thread_t *status = (thread_t*)args;

    while(1){
        pthread_mutex_lock(&status->lock); //加锁
        while(status->que.size == 0 && status->exit_flag == 0){
            //说明任务队列无任务并且退出标记位为0
            pthread_cond_wait(&status->cond,&status->lock);
        }

        if(status->exit_flag == 1){
            //说明主线程要求子线程退出
            pthread_mutex_unlock(&status->lock);
            printf("子线程退出...\n");
            pthread_exit(0);
        }

        //到这里说明了任务队列有任务
        int client_fd = status->que.head->client_fd;
        dequeue(&status->que); //出队一个元素
        pthread_mutex_unlock(&status->lock); //释放锁
        
        //先读socket
        data cur_msg;

        //服务端当前的工作目录
        char work_path[1024] = {0};
        char new_path[1024] = {0};
        //printf("work_path = %s\n", work_path);
        //存用户名
        char user_name[20] = {0};

        //初始化数据库连接
        MYSQL *mysql;
        mysql = mysql_init(NULL);
        printf("mysql_error:%s\n",mysql_error(mysql));
        //建立数据库连接
        mysql_real_connect(mysql,"localhost","root","123456","disk",0,NULL,0);
        printf("mysql_error:%s\n",mysql_error(mysql));
        mysql_set_character_set(mysql,"utf8mb4");
        while(1){
            memset(&cur_msg,0,sizeof(cur_msg));
            //读取数据长度
            int ret = recv(client_fd,&cur_msg,sizeof(cur_msg),MSG_WAITALL);
            printf("cur_msg.command:%s\n",cur_msg.command);
            printf("cur_msg.args:%s\n",cur_msg.args);
            //temp_buf用来临时记录cur_msg.args参数
            char temp_buf[1024] = {0};
            memcpy(temp_buf,cur_msg.args,strlen(cur_msg.args));
            //分割出来的用户名
            char *token = strtok(temp_buf,"&");
            if(token != NULL && (strcmp(user_name,"") == 0)){
                //获取用户名
                memcpy(user_name,token,strlen(token));
            }
            if(ret == 0){
                printf("客户端关闭...\n");
                char log_buf[1024] = {0};
                sprintf(log_buf,"(%s) 客户端关闭连接...\n",user_name);
                log_log("INFO","CLIENT",log_buf);
                //关闭文件描述符
                close(client_fd);
                //关闭数据库连接
                mysql_close(mysql);
                break;
            }
            //获取用户输入的指令
            log_log_with_data("INFO","CLIENT",user_name,&cur_msg);
            if(strcmp(cur_msg.command,"sigup") == 0){
                handler_sigup(client_fd,cur_msg,work_path,mysql,user_name);
            }else if(strcmp(cur_msg.command,"login") == 0){
                handler_login(client_fd,cur_msg,work_path,mysql,user_name,new_path);
            }else if(strcmp(cur_msg.command,"ls") == 0){
                //ls命令
                handler_ls(user_name, &cur_msg, client_fd, mysql);
            }else if(strcmp(cur_msg.command,"cd") == 0){
                //printf("cd接收到了命令\n");
                //cd命令处理
                handler_cd(client_fd, cur_msg, new_path, user_name, mysql, work_path);
            }else if(strcmp(cur_msg.command,"rm") == 0){
                //remove命令处理函数 Li
                handler_remove( client_fd, &cur_msg,  work_path,  user_name,  mysql);
            }else if(strcmp(cur_msg.command,"gets") == 0){
                //char file_name[10] = {0};
                //memcpy(file_name,cur_msg.args,strlen(cur_msg.args));
                handler_gets(client_fd, cur_msg, new_path, mysql, user_name);
            }else if(strcmp(cur_msg.command,"puts") == 0){
                handler_puts(client_fd, cur_msg, user_name, mysql);
            }else if(strcmp(cur_msg.command,"mkdir") == 0){
                // mkdir命令
                handler_mkdir(user_name, &cur_msg, client_fd, mysql);
            }
        }
    }
    return 0;
}